package com.java.actual.huarongdao;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class HuaRongDao extends JFrame implements MouseMotionListener,KeyListener{
	private Person[] person;
	private JButton left,right,above,below,restart;
	public HuaRongDao(){
		super("决战华容道！");
		this.person=new Person[10];
		left=new JButton();
		right=new JButton();
		above=new JButton();
		below=new JButton();
		restart=new JButton("重新开始..");
		initial();
		addEventHandler();
	}
	public void initial(){
		this.setLayout(null);
		String[] name={"曹操","关羽","张飞","刘备","赵云","黄忠","兵","兵","兵","兵"};
		for(int i=0;i<person.length;i++){
			person[i]=new Person(i,name[i]);
			this.add(person[i]);
		}
		add(left);
		add(right);
		add(above);
		add(below);
		add(restart);
		
		setPersonBounds();
	}
	public void setPersonBounds(){
		person[0].setBounds(104, 54, 100, 100);
		person[1].setBounds(104, 154, 100, 50);
		person[2].setBounds(54, 154, 50, 100);
		person[3].setBounds(204, 154, 50, 100);
		person[4].setBounds(54, 54, 50, 100);
		person[5].setBounds(204, 54, 50, 100);
		person[6].setBounds(54, 254, 50, 50);
		person[7].setBounds(204, 254, 50, 50);
		person[8].setBounds(104, 204, 50, 50);
		person[9].setBounds(154, 204, 50, 50);
		
		left.setBounds(49, 49, 5, 260);
		right.setBounds(254, 49, 5, 260);
		above.setBounds(49, 49, 210, 5);
		below.setBounds(49, 304, 210, 5);
		
		restart.setBounds(100, 320, 120, 25);
		this.validate();
	}
	public void showMe(){
		this.pack();
		this.setVisible(true);
		this.setBounds(350, 150, 320, 400);
		this.validate();
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}
	public void addEventHandler(){
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				int op=JOptionPane.showConfirmDialog(HuaRongDao.this, "你确定退出游戏吗？", "退出游戏对话框", JOptionPane.YES_NO_OPTION);
				if(op==JOptionPane.YES_OPTION){
					System.exit(0);
				}
			}
			
		});
		
		this.restart.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				HuaRongDao.this.dispose();
				new HuaRongDao().showMe();
			}
			
		});
		for(int i=0;i<this.person.length;i++){
			person[i].addMouseMotionListener(this);
			person[i].addKeyListener(this);
		}
	}
	
	public void keyPressed(KeyEvent e) {
		//getSource()得到产生该键盘事件的事件源（组件），即Person
		Person man=(Person)e.getSource();
		if(e.getKeyCode()==KeyEvent.VK_DOWN){
			move(man,below);
		}
		if(e.getKeyCode()==KeyEvent.VK_UP){
			move(man,above);
		}
		if(e.getKeyCode()==KeyEvent.VK_LEFT){
			move(man,left);
		}
		if(e.getKeyCode()==KeyEvent.VK_RIGHT){
			move(man,right);
		}
	}
	public void mouseDragged(MouseEvent e) {
		Person man=(Person)e.getSource();
		int x=-1,y=-1;
		x=e.getX();
		y=e.getY();
		int w=man.getBounds().width;
		int h=man.getBounds().height;
		if(y>h/2){
			move(man,below);
		}
		if(y<h/2){
			move(man,above);
		}
		if(x<w/2){
			move(man,left);
		}
		if(x>w/2){
			move(man,right);
		}
	}
	
	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}
	public void mouseMoved(MouseEvent e) {}
	
	
	public void move(Person man,JButton direction){
		//flag表示是否可以移动
		boolean flag=true;
		//返回按钮的矩形区域对象
		Rectangle manRect=man.getBounds();
		int x=manRect.x;
		int y=manRect.y;
		if(direction==below){
			y+=50;
		}else if(direction==above){
			y-=50;
		}else if(direction==left){
			x-=50;
		}else if(direction==right){
			x+=50;
		}
		
		manRect.setLocation(x, y);
		Rectangle directionRect=direction.getBounds();
		for(int i=0;i<person.length;i++){
			Rectangle personRect=person[i].getBounds();
			if(manRect.intersects(personRect)){
				flag=false;
			}
		}
		if(manRect.intersects(directionRect)){
			flag=false;
		}
		if(flag){
			man.setLocation(x, y);
		}
	}
	public static void main(String[] args){
		new HuaRongDao().showMe();
	}


}
